package com.bfly.mall.product.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.mall.product.dao.ProductImgMapper;
import com.bfly.mall.product.entity.ProductImg;
import com.bfly.mall.product.dto.ProductImgFormDTO;
import com.bfly.mall.product.service.IProductImgService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 12:32
 */
@Service
public class ProductImgServiceImpl extends BaseServiceImpl<ProductImg, Integer> implements IProductImgService {

    @Autowired
    private ProductImgMapper imgMapper;

    @Override
    public List<ProductImg> getImgs(int productId) {
        return imgMapper.getImgs(productId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void merge(int productId, List<ProductImgFormDTO> imgs) {
        // 产品图片先清空 然后新增
        imgMapper.clearImgs(productId);
        imgs.forEach(formDTO -> {
            ProductImg img = new ProductImg();
            BeanUtils.copyProperties(formDTO, img);
            img.setProductId(productId);
            img.setCreateDate(new Date());
            img.setUpdateDate(img.getCreateDate());
            super.save(img);
        });
    }
}